<template>
  <div class="employee-select">
    <header id="header" class="mui-bar mui-bar-nav">
      <a class="mui-icon mui-icon-left-nav mui-pull-left" @tap="back()"></a>
      <h1 class="mui-title" v-text="currentHeader.title"></h1>
      <a class="mui-btn mui-btn-link mui-pull-right" @tap="confirm()">确定</a>
    </header>
    <div class="mui-content">
      <search-select :type="type"></search-select>
      <dept-list></dept-list>
    </div>
  </div>
</template>
<style lang="less" rel="stylesheet/less">
  @import './index.css';
</style>
<script>
  //  require('db.sqlite');
  // 公共的数据
  import store from './em-store';
  // 加载搜索+选择框
  import SearchSelected from './search-selected.vue';
  //  部门列表
  import DeptList from './dept-list.vue';

  export default {
    components: {
      'search-select': SearchSelected,
      'dept-list': DeptList
    },
    data: function () {
      return {
        currentHeader: {
          title: '部门选择'
        },
        index: store.state,
        currentIndex: 0,
        type: 'dept'
      };
    },
    methods: {
      back: function () {
        var self = this;
        self.index.isRefresh = true;
        history.go(-1);
      },
      confirm: function () {
        var self = this,
          param = self.$route.params.fun,
          lists = store.state.personSelectList,
          deptList = store.state.deptList,
          deptSelectList = [];
        if (lists.length == 0) {
          mui.alert('请选择人员！', '提示', function () {
          });
          return;
        }
        window[param](lists);
        for (var i = 0; i < deptList.length; i++) {
          if (deptList[i].selected) {
            deptSelectList.push(deptList[i]);
          }
        }
        sessionStorage.setItem(param, JSON.stringify({
          'emp': lists,
          'dept': deptSelectList
        }));
        self.back();
      }
    },
    created: function () {
      store.initDeptEmp();
    },
    route: {
      activate: function () {
        /*只在新进入该页面时调用*/
        var self = this,
          param = self.$route.params.fun,
          radio = self.$route.params.radio;
        var dataJson = JSON.parse(sessionStorage.getItem(param) || '{}');
        self.index.isRadio = radio && radio === '1';
        if (self.index.isRefresh) {
          store.initSetDeptEmp(dataJson);
        }
      }
    },
    ready: function () {
    }
  }
</script>
